/*****************************************************************
 *@Copyright all rights reserved © 2014 Yuga
 *@author Oplink OMS
 *Contact: zengkang007@gmail.com
 *****************************************************************/
package com.oplink.webbase.modules.core.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oplink.webbase.common.persistence.Page;
import com.oplink.webbase.modules.core.dao.IndexDao;
import com.oplink.webbase.modules.core.entity.Index;

/**
 * 告警Service
 * 
 * @author Oplink OMS
 * @version 2014-08-11
 */
@Component
@Transactional
public class IndexService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(IndexService.class);

	@Autowired
	private IndexDao indexDao;

	public Index get(Long id) {
		return indexDao.findOne(id);
	}

	public Page<Index> find(Page<Index> page, Index index) {
		DetachedCriteria dc = indexDao.createDetachedCriteria();
		// 添加查询条件
		if (StringUtils.isNotEmpty(index.getName())) {
			dc.add(Restrictions.like("name", "%" + index.getName() + "%"));
		}
		if (StringUtils.isNotEmpty(index.getFormat())) {
			dc.add(Restrictions.like("format", "%" + index.getFormat() + "%"));
		}
		dc.add(Restrictions.eq(Index.DEL_FLAG, Index.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		// return indexDao.find(page, dc);
		return null;
	}

	/**
	 * @function 创建查询
	 **/
	public Page<Index> getUserIndex(Page<Index> page, Index index) {
		DetachedCriteria dc = indexDao.createDetachedCriteria();
		// 添加查询条件
		if (StringUtils.isNotEmpty(index.getName())) {
			dc.add(Restrictions.like("name", "%" + index.getName() + "%"));
		}
		if (StringUtils.isNotEmpty(index.getFormat())) {
			dc.add(Restrictions.like("format", "%" + index.getFormat() + "%"));
		}
		dc.add(Restrictions.eq(Index.DEL_FLAG, Index.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return indexDao.find(page, dc);
	}

	public Index getIndex(Long id) {
		return indexDao.findOne(id);
	}

	public void saveIndex(Index entity) {
		indexDao.save(entity);
	}

	public void deleteIndex(Long id) {
		indexDao.delete(id);
	}

	@Autowired
	public void setIndexDao(IndexDao indexDao) {
		this.indexDao = indexDao;
	}
}
